<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组去重</title>
    <script>
        // 练习 有如下一个数组  arr=[1,2,1,3,2,4,5,5,6,7]编写代码去除重复元素
        
        const arr1 = [1, 2, 2,2,2,1, 3, 2, 4, 5, 5, 6, 7]

        const fn = (array) => {
            const new_array = []
            for (let i = 0; i < array.length; i++) {    
            // .includes()如果一个数组中包含一个元素,则返回true,没有则返回false
                if (new_array.includes(array[i]) == false) {    
                    new_array.push(array[i])
                }
            }
            return new_array
        }



        const fn2=(array)=>{
            const new_arrray=[]
            for (let i of array){
            // .indexOf()用来查询元素在数组里第一次出现的索引，没有就返回-1
                if(new_arrray.indexOf(i)==-1){
                    new_arrray.push(i)
                }
            }
            return new_arrray
        }
        console.log(fn(arr1))
        console.log(fn2(arr1))



        const  fn3=(array)=>{
            const  new_arrray=[...(new Set(array))]

            return  new_arrray
        }

        console.log(fn3(arr1))


    </script>
</head>

<body>

</body>

</html>